/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Actors;

import graphics.Images;
import graphics.RenderInstruction;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.Transparency;
import java.awt.image.VolatileImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import resources.images.ResourceStub;
import stealth.Main;

/**
 *
 * @author Joshua
 */
public class EnemyActor extends Actor {

    public EnemyActor() {
        x = Main.game.getWidth() - (Math.random() * 10);
        if (Math.random() > .5) {
            y = minY;
        } else {
            y = maxY;
        }
    }
    final int minY = 60, maxY = 675;
    int speed = 7;
    public boolean die = false;

    @Override
    public void act() {
        y += speed;
        if (y > maxY) {
            x -= (speed * speed);
            speed *= -1;
            y = maxY;
        }
        if (y < minY) {
            x -= (speed * speed);
            speed *= -1;
            y = minY;
            if (x < 0) {
                die = true;
            }
        }
    }

    @Override
    public RenderInstruction render() {
        return new EnemyActorRender(this);
    }

    @Override
    public void collide(Actor collidedWith) {
        if (collidedWith instanceof PlayerActor) {
        } else if (collidedWith instanceof ShotActor) {
            die = true;
        }
    }

    @Override
    public Shape[] collidableArea() {

        Polygon p = new Polygon();
        if (!die) {
            p.addPoint((int) x, (int) y);
            p.addPoint((int) x + 96, (int) y);
            p.addPoint((int) x, (int) y + 96);
            p.addPoint((int) x + 96, (int) y + 96);
        }
        Shape[] s = new Shape[1];
        s[0] = p;
        return s;
    }
}

class EnemyActorRender extends RenderInstruction {

    EnemyActor actor;
    private static VolatileImage enemyImage;

    public EnemyActorRender(EnemyActor a) {
        actor = a;
        if (enemyImage == null || enemyImage.contentsLost()) {
            enemyImage = Images.createVolatileImage(96, 96, Transparency.BITMASK);
            Graphics2D g = (Graphics2D) enemyImage.getGraphics();
            g.setComposite(AlphaComposite.Src);
            g.setColor(new Color(0, 0, 0, 0));
            g.fillRect(0, 0, enemyImage.getWidth(), enemyImage.getHeight());
            try {
                g.drawImage(ImageIO.read(ResourceStub.class.getResource("enemySprite.png")), 0, 0, null);
            } catch (IOException ex) {
                System.err.println("Could not load file!");
                System.exit(1);
            }
            g.dispose();
        }
    }

    @Override
    public void render(Graphics2D g2d) {
        if (actor.die) {
            return;
        }
        if (enemyImage == null || enemyImage.contentsLost()) {
            enemyImage = Images.createVolatileImage(96, 96, Transparency.BITMASK);
            Graphics2D g = (Graphics2D) enemyImage.getGraphics();
            g.setComposite(AlphaComposite.Src);
            g.setColor(new Color(0, 0, 0, 0));
            g.fillRect(0, 0, enemyImage.getWidth(), enemyImage.getHeight());
            try {
                g.drawImage(ImageIO.read(ResourceStub.class.getResource("enemySprite.png")), 0, 0, null);
            } catch (IOException ex) {
                System.err.println("Could not load file!");
                System.exit(1);
            }
            g.dispose();
        }

        g2d.setColor(Color.GREEN);
        //g2d.draw(actor.collidableArea()[0]);
        //g2d.drawString("X: " + actor.x + " Y: " + actor.y, (int) actor.x + 12, (int) actor.y + 12);
        g2d.drawImage(enemyImage, (int) actor.x, (int) actor.y, null);
        g2d.setColor(Color.WHITE);
    }
}
